package com.qf.admin.intetceptor;

import com.qf.common.constant.Constants;
import com.qf.common.http.ResultCode;
import com.qf.entiy.dto.Admin;
import com.qf.entiy.po.LoginAdmin;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//拦截器
public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从subject中,获取当前登录用户的信息,放入request保存
        Subject subject = SecurityUtils.getSubject();
        LoginAdmin loginAdmin = (LoginAdmin) subject.getPrincipal();

        if (loginAdmin == null){
            throw new RuntimeException(ResultCode.LOGIN_ERROR.getDescription());
        }
        request.setAttribute(Constants.CURRENT_ADMIN,loginAdmin);
        return super.preHandle(request, response, handler);
    }
}
